package com.mmall.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.mmall.common.ServiceResponse;
import com.mmall.dao.CategoryMapper;
import com.mmall.pojo.Category;
import com.mmall.service.ICategoryService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;


/**
 * Created by sc on 2017/8/17.
 */
@Service("iCategoryService")
public class CategoryServiceImpl implements ICategoryService {

    private Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public ServiceResponse addCategory(String categoryName, Integer parentId) {
        if (parentId == null || StringUtils.isBlank(categoryName)) {
            return ServiceResponse.createByErrorMessage("添加品类错误");
        }
        Category category = new Category();
        category.setName(categoryName);
        category.setStatus(true);
        category.setParentId(parentId);
        int row = categoryMapper.insertSelective(category);
        if (row > 0) {
            return ServiceResponse.createBySuccessMessage("添加成功");
        }
        return ServiceResponse.createByErrorMessage("添加失败");
    }

    @Override
    public ServiceResponse setCategoryName(Integer parentId, String categoryname) {
        if (parentId == null || StringUtils.isBlank(categoryname)) {
            return ServiceResponse.createByErrorMessage("更新品类错误");
        }
        Category category = new Category();
        category.setName(categoryname);
        category.setId(parentId);
        int row = categoryMapper.updateByPrimaryKeySelective(category);
        if (row > 0) {
            return ServiceResponse.createBySuccessMessage("修改成功");
        }
        return ServiceResponse.createByErrorMessage("修改失败");
    }

    @Override
    public ServiceResponse<List<Category>> getChildrenParalleCategory(Integer categoryid) {
        List<Category> categoryList = categoryMapper.selectChildrenParalleCategory(categoryid);
        if (CollectionUtils.isEmpty(categoryList)) {
            logger.info("未找到子分类");
        }
        return ServiceResponse.createBySuccess(categoryList);

    }

    @Override
    public ServiceResponse<List<Integer>> getCategoryAndDeepChildrenCategory(Integer categoryid) {
        Set<Category> categorySet = Sets.newHashSet();
        findChildCategory(categorySet, categoryid);
        List<Integer> list = Lists.newArrayList();
        if (categoryid != null) {
            for (Category categoryItem : categorySet) {
                list.add(categoryItem.getId());
            }
        }
        return ServiceResponse.createBySuccess(list);
    }

    private Set<Category> findChildCategory(Set<Category> categoryset, Integer categoryid) {
        //递归找子节点
        Category category = categoryMapper.selectByPrimaryKey(categoryid);
        if (category != null) {
            categoryset.add(category);
        }
        List<Category> categoryList = categoryMapper.selectChildrenParalleCategory(categoryid);
        for (Category categoryItem : categoryList) {
            findChildCategory(categoryset, categoryItem.getId());
        }
        return categoryset;
    }
}
